<?php

namespace app\admin\controller\xilujob;

use app\common\controller\Backend;
use think\Config;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 图片banner
 *
 * @icon fa fa-circle-o
 */
class UserStatistics extends Backend
{
    
    /**
     * Banner模型对象
     * @var \app\admin\model\xilujob\Banner
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
//        $year = date('Y');
//        $start_year = $year - 10;
//        $years = [];
//        for($i=$start_year;$i<=$year;$i++){
//            $years[] = $i;
//        }
//        $this->assignconfig('years',$years);
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            $filter = $this->request->get("filter", '');
            if($filter){
                $filter = json_decode($filter,true);
                $year = isset($filter['year']) ? $filter['year'] : date('Y');
            }else{
                $year = date('Y');
            }
            if($year == date('Y')){
                $starttime = strtotime($year.'-01-01 23:59:59');
                $endtime = strtotime(date('Y-m-d 23:59:59'));
            }else{
                $starttime = strtotime($year.'-01-01 23:59:59');
                $endtime = strtotime($year.'-12-31 23:59:59');
            }
            $diff = date("m",$endtime) - date("m",$starttime) + 1;
            $series = [];
            for ($i = $diff ; $i >= 1;$i--){
                $key = $year.'-'.($i < 10 ? '0'.$i : $i);
                $series[$key] = [
                    'date'              =>  $key,
                    'year'              =>  $year,
                    'company_count'     =>  0,
                    'job_count'         =>  0,
                    'download_count'    =>  0,
                    'refresh_count'     =>  0,
                    'member_count'      =>  0,
                    'apply_count'       =>  0,
                ];
            }
            $companyModel = new \app\admin\model\xilujob\CompanyProfile();
            $format = '%Y-%m';
            //1.企业新增
            $field = "from_unixtime(createtime, '{$format}') AS dates, COUNT(id) as num";
            $map = [
                'createtime'    => ['between', [$starttime, $endtime]]
            ];
            $lists = $companyModel->field($field)->where($map)->group('dates')->select();
            foreach($lists as $v){
                $series[$v['dates']]['company_count'] = $v['num'];
            }
            unset($v);
            //2.职位新增
            $jobModel = new \app\admin\model\xilujob\Job();
            $field2 = "from_unixtime(createtime, '{$format}') AS dates, COUNT(id) as num";
            $map2 = [
                'createtime'    => ['between', [$starttime, $endtime]]
            ];
            $lists = $jobModel->field($field2)->where($map2)->group('dates')->select();
            foreach($lists as $v){
                $series[$v['dates']]['job_count'] = $v['num'];
            }
            unset($v);

            //3.简历下载
            $downloadModel = new \app\common\model\xilujob\ResumeDownload();
            $field3 = "from_unixtime(createtime, '{$format}') AS dates, COUNT(id) as num";
            $map3 = [
                'createtime'    => ['between', [$starttime, $endtime]]
            ];
            $lists = $downloadModel->field($field3)->where($map3)->group('dates')->select();
            foreach($lists as $v){
                $series[$v['dates']]['download_count'] = $v['num'];
            }
            unset($v);

            //4.刷新次数
            $refreshLogModel = new \app\common\model\xilujob\JobRefreshLog();
            $field4 = "from_unixtime(createtime, '{$format}') AS dates, COUNT(id) as num";
            $map4 = [
                'createtime'    => ['between', [$starttime, $endtime]]
            ];
            $lists = $refreshLogModel->field($field4)->where($map4)->group('dates')->select();
            foreach($lists as $v){
                $series[$v['dates']]['refresh_count'] = $v['num'];
            }
            unset($v);

            //5.新增求职者
            $memberModel = new \app\common\model\xilujob\Member();
            $field5 = "from_unixtime(createtime, '{$format}') AS dates, COUNT(id) as num";
            $map5 = [
                'createtime'    => ['between', [$starttime, $endtime]]
            ];
            $lists = $memberModel->field($field5)->where($map5)->group('dates')->select();
            foreach($lists as $v){
                $series[$v['dates']]['member_count'] = $v['num'];
            }
            unset($v);

            //6.新增投递
            $jobApplyModel = new \app\common\model\xilujob\JobApply();
            $field5 = "from_unixtime(createtime, '{$format}') AS dates, COUNT(id) as num";
            $map5 = [
                'createtime'    => ['between', [$starttime, $endtime]]
            ];
            $lists = $jobApplyModel->field($field5)->where($map5)->group('dates')->select();
            foreach($lists as $v){
                $series[$v['dates']]['apply_count'] = $v['num'];
            }
            unset($v);

            $list = array_values($series);
            $result = array("total" => 0, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }


    public function detailed()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {

            $date = $this->request->get('date');
            $starttime = strtotime($date.'-01');
            $endtime = strtotime(date('Y-m-d', mktime(23, 59, 59, date('m', strtotime($date))+1, 00)));
            $diff = date("d",$endtime) - date("d",$starttime) + 1;
            $series = [];
            for ($i = $diff ; $i >= 1;$i--){
                $key = $date.'-'.($i < 10 ? '0'.$i : $i);
                $series[$key] = [
                    'date'              =>  $key,
                    'company_count'     =>  0,
                    'job_count'         =>  0,
                    'download_count'    =>  0,
                    'refresh_count'     =>  0,
                    'member_count'      =>  0,
                    'apply_count'       =>  0,
                ];
            }

            $companyModel = new \app\admin\model\xilujob\CompanyProfile();
            $format = '%Y-%m-%d';
            //1.企业新增
            $field = "from_unixtime(createtime, '{$format}') AS dates, COUNT(id) as num";
            $map = [
                'createtime'    => ['between', [$starttime, $endtime]]
            ];
            $lists = $companyModel->field($field)->where($map)->group('dates')->select();
            foreach($lists as $v){
                $series[$v['dates']]['company_count'] = $v['num'];
            }
            unset($v);
            //2.职位新增
            $jobModel = new \app\admin\model\xilujob\Job();
            $field2 = "from_unixtime(createtime, '{$format}') AS dates, COUNT(id) as num";
            $map2 = [
                'createtime'    => ['between', [$starttime, $endtime]]
            ];
            $lists2 = $jobModel->field($field2)->where($map2)->group('dates')->select();
            foreach($lists2 as $v){
                $series[$v['dates']]['job_count'] = $v['num'];
            }
            unset($v);

            //3.简历下载
            $downloadModel = new \app\common\model\xilujob\ResumeDownload();
            $field3 = "from_unixtime(createtime, '{$format}') AS dates, COUNT(id) as num";
            $map3 = [
                'createtime'    => ['between', [$starttime, $endtime]]
            ];
            $lists3 = $downloadModel->field($field3)->where($map3)->group('dates')->select();
            foreach($lists3 as $v){
                $series[$v['dates']]['download_count'] = $v['num'];
            }
            unset($v);

            //4.刷新次数
            $refreshLogModel = new \app\common\model\xilujob\JobRefreshLog();
            $field4 = "from_unixtime(createtime, '{$format}') AS dates, COUNT(id) as num";
            $map4 = [
                'createtime'    => ['between', [$starttime, $endtime]]
            ];
            $lists4 = $refreshLogModel->field($field4)->where($map4)->group('dates')->select();
            foreach($lists4 as $v){
                $series[$v['dates']]['refresh_count'] = $v['num'];
            }
            unset($v);

            //5.新增求职者
            $memberModel = new \app\common\model\xilujob\Member();
            $field5 = "from_unixtime(createtime, '{$format}') AS dates, COUNT(id) as num";
            $map5 = [
                'createtime'    => ['between', [$starttime, $endtime]]
            ];
            $lists5 = $memberModel->field($field5)->where($map5)->group('dates')->select();
            foreach($lists5 as $v){
                $series[$v['dates']]['member_count'] = $v['num'];
            }
            unset($v);

            //6.新增投递
            $jobApplyModel = new \app\common\model\xilujob\JobApply();
            $field5 = "from_unixtime(createtime, '{$format}') AS dates, COUNT(id) as num";
            $map5 = [
                'createtime'    => ['between', [$starttime, $endtime]]
            ];
            $lists6 = $jobApplyModel->field($field5)->where($map5)->group('dates')->select();
            foreach($lists6 as $v){
                $series[$v['dates']]['apply_count'] = $v['num'];
            }
            unset($v);

            $list = array_values($series);
            $result = array("total" => 0, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

}
